const  connectiondb = require("../sql/index");
const  jwt = require("jsonwebtoken");
// token令牌与过期时间
const  {jwtsign,expiresIn} = require("../utils/index");


/* 
    TODO 获取所有的数据信息
*/
async function querylendlist(req, res) {
    const result = await connectiondb("select * from lend_list", [])
    res.send({ code: 200, msg: "获取学生信息成功", data: result })
}

/*
   TODO 根据id获取数据信息
*/
async function idlendlist(req,res){
    const { id } = req.body;
    const sql = "select * from lend_list where id = ?"
    const a = await connectiondb(sql, [ id ])
    res.send({ code: 200, msg: '获取id成功',data:a[0]})
}



/*
   TODO 删除借阅图书信息
*/
async function dellendlist(req,res){
    const { id } = req.body;
    const sql = "delete from lend_list where id = ?"
    const a = await connectiondb(sql, [ id ])
    // console.log(a);
    res.send({ code: 200, msg: '删除成功'})
}



/*
   TODO 添加借阅图书信息
*/
async function addlendlist(req, res) {
    const { reader, book, lendtime, backtime,price,state } = req.body;
    const sql = "insert into lend_list value(null,?,?,?,?,?,?)";
    await connectiondb(sql, [ reader, book, lendtime, backtime, price, state])
    res.send({ code: 200, msg: "添加信息成功" })
}



/*
   TODO 修改借阅图书信息
*/

// 点击修改获取id  根据id再后台拿取对应的数据（15行）

async function updatelendlist(req, res) {
    const {  id1, reader1, book1, lendtime1, backtime1, price1, state1 } = req.body;
    const sql = "update lend_list set reader_id = ? , book_ref = ? , lend_time = ? , back_time = ? , price = ? , state = ?  where id = ?"
    await connectiondb(sql, [id1, reader1, book1, lendtime1, backtime1, price1, state1])
    res.send({code:200,msg:"修改信息成功"})
}



module.exports = {
    querylendlist,
    addlendlist,
    dellendlist,
    idlendlist,
    updatelendlist
}